#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stdexcept>


// Token类型枚举
enum TokenType {
    KEYWORD,    // 保留字
    IDENTIFIER, // 标识符
    NUMBER,     // 数字
    OPERATOR,   // 运算符
    DELIMITER,  // 界符
    EOF_TOKEN   // 结束标志
};

// Token结构体
struct Token {
    TokenType type;
    string value;
    Token(TokenType t, const string& v) : type(t), value(v) {}
};


class Parser {
    private:
        vector<Token> tokens;
        int pos;
        Token current_token;
    
        void eat(TokenType expected_type, const string& expected_value = "") {
            if (current_token.type == expected_type && 
                (expected_value.empty() || current_token.value == expected_value)) {
                pos++;
                if (pos < tokens.size()) {
                    current_token = tokens[pos];
                } else {
                    current_token = Token(EOF_TOKEN, "");
                }
            } else {
                throw runtime_error("语法错误: 期望 " + expected_value + 
                                   ", 实际得到 " + current_token.value);
            }
        }
    
    public:
        Parser(const vector<Token>& t) : tokens(t), pos(0) {
            if (!tokens.empty()) {
                current_token = tokens[0];
            } else {
                current_token = Token(EOF_TOKEN, "");
            }
        }
    
        void parse_program() {
            parse_block();
            eat(DELIMITER, ".");
        }
    
        void parse_block() {
            if (current_token.value == "const") {
                parse_const_declaration();
            }
            if (current_token.value == "var") {
                parse_var_declaration();
            }
            if (current_token.value == "procedure") {
                parse_procedure_declaration();
            }
            parse_statement();
        }
    
        void parse_const_declaration() {
            eat(KEYWORD, "const");
            parse_const_definition();
            while (current_token.value == ",") {
                eat(DELIMITER, ",");
                parse_const_definition();
            }
            eat(DELIMITER, ";");
        }
    
        void parse_const_definition() {
            eat(IDENTIFIER);
            eat(OPERATOR, "=");
            eat(NUMBER);
        }
    
        void parse_statement() {
            if (current_token.type == IDENTIFIER) {
                // 赋值语句
                eat(IDENTIFIER);
                eat(OPERATOR, ":=");
                parse_expression();
            } else if (current_token.value == "call") {
                // 过程调用
                eat(KEYWORD, "call");
                eat(IDENTIFIER);
            } else if (current_token.value == "begin") {
                // 复合语句
                eat(KEYWORD, "begin");
                parse_statement();
                while (current_token.value == ";") {
                    eat(DELIMITER, ";");
                    parse_statement();
                }
                eat(KEYWORD, "end");
            }
            // 其他语句（如if、while）可扩展
        }
    
        void parse_expression() {
            if (current_token.value == "+" || current_token.value == "-") {
                eat(OPERATOR);
            }
            parse_term();
            while (current_token.value == "+" || current_token.value == "-") {
                eat(OPERATOR);
                parse_term();
            }
        }
    
        void parse_term() {
            parse_factor();
            while (current_token.value == "*" || current_token.value == "/") {
                eat(OPERATOR);
                parse_factor();
            }
        }
    
        void parse_factor() {
            if (current_token.type == IDENTIFIER) {
                eat(IDENTIFIER);
            } else if (current_token.type == NUMBER) {
                eat(NUMBER);
            } else if (current_token.value == "(") {
                eat(DELIMITER, "(");
                
                parse_expression();
                eat(DELIMITER, ")");
            } else {
                throw runtime_error("非法的因子");
            }
        }
    };
    
    // 测试代码
    int main() {
        // 假设已通过词法分析器生成tokens
        vector<Token> tokens = { /* 填充词法分析结果 */ };
    
        try {
            Parser parser(tokens);
            parser.parse_program();
            cout << "语法分析成功！程序符合PL/0语法规则。" << endl;
        } catch (const runtime_error& e) {
            cerr << "语法错误: " << e.what() << endl;
            return 1;
        }
        return 0;
    }